package com.myKng.search.controller;

import com.myKng.search.model.KnowledgeDocument;
import com.myKng.search.service.SearchService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 搜索控制器
 * 提供知识内容搜索的API接口
 */
@Slf4j
@RestController
@RequestMapping("/api/search")
@RequiredArgsConstructor
public class SearchController {

    private final SearchService searchService;

    /**
     * 基础搜索接口
     *
     * @param keyword 关键词
     * @param page    页码
     * @param size    每页大小
     * @return 搜索结果
     */
    @GetMapping("/basic")
    public ResponseEntity<Page<KnowledgeDocument>> basicSearch(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        
        log.info("执行基础搜索，关键词: {}, 页码: {}, 大小: {}", keyword, page, size);
        Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "updateTime"));
        Page<KnowledgeDocument> results = searchService.searchByKeyword(keyword, pageable);
        return ResponseEntity.ok(results);
    }

    /**
     * 高级搜索接口
     *
     * @param params  搜索参数
     * @param page    页码
     * @param size    每页大小
     * @return 搜索结果
     */
    @PostMapping("/advanced")
    public ResponseEntity<Page<KnowledgeDocument>> advancedSearch(
            @RequestBody Map<String, Object> params,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        
        log.info("执行高级搜索，参数: {}, 页码: {}, 大小: {}", params, page, size);
        Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "updateTime"));
        Page<KnowledgeDocument> results = searchService.advancedSearch(params, pageable);
        return ResponseEntity.ok(results);
    }

    /**
     * 按文件类型搜索
     *
     * @param fileType 文件类型
     * @param page     页码
     * @param size     每页大小
     * @return 搜索结果
     */
    @GetMapping("/by-type/{fileType}")
    public ResponseEntity<Page<KnowledgeDocument>> searchByFileType(
            @PathVariable String fileType,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        
        log.info("按文件类型搜索，类型: {}, 页码: {}, 大小: {}", fileType, page, size);
        Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "updateTime"));
        Page<KnowledgeDocument> results = searchService.searchByFileType(fileType, pageable);
        return ResponseEntity.ok(results);
    }

    /**
     * 按标签搜索
     *
     * @param tag  标签
     * @param page 页码
     * @param size 每页大小
     * @return 搜索结果
     */
    @GetMapping("/by-tag/{tag}")
    public ResponseEntity<Page<KnowledgeDocument>> searchByTag(
            @PathVariable String tag,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        
        log.info("按标签搜索，标签: {}, 页码: {}, 大小: {}", tag, page, size);
        Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "updateTime"));
        Page<KnowledgeDocument> results = searchService.searchByTag(tag, pageable);
        return ResponseEntity.ok(results);
    }

    /**
     * 获取搜索建议
     *
     * @param prefix 前缀
     * @param limit  限制数量
     * @return 建议列表
     */
    @GetMapping("/suggestions")
    public ResponseEntity<List<String>> getSuggestions(
            @RequestParam String prefix,
            @RequestParam(defaultValue = "5") int limit) {
        
        log.info("获取搜索建议，前缀: {}, 限制: {}", prefix, limit);
        List<String> suggestions = searchService.getSuggestions(prefix, limit);
        return ResponseEntity.ok(suggestions);
    }

    /**
     * 获取相关推荐
     *
     * @param documentId 文档ID
     * @param limit      限制数量
     * @return 推荐文档列表
     */
    @GetMapping("/related/{documentId}")
    public ResponseEntity<List<KnowledgeDocument>> getRelatedDocuments(
            @PathVariable String documentId,
            @RequestParam(defaultValue = "5") int limit) {
        
        log.info("获取相关推荐，文档ID: {}, 限制: {}", documentId, limit);
        List<KnowledgeDocument> relatedDocs = searchService.getRelatedDocuments(documentId, limit);
        return ResponseEntity.ok(relatedDocs);
    }
}